﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SalesPortal.Web
{
    public static class FormCollectionExtensions
    {
        public static bool IsNotNullOrEmpty(this FormCollection formCollection, string key)
        {
            return !string.IsNullOrEmpty(formCollection[key].Trim());
        }

        public static string ParsedText(this FormCollection formCollection, string key)
        {
            return formCollection[key].Trim();
        }

        public static DateTime ParsedDate(this FormCollection formCollection, string key, DateTime defaultValue)
        {
            DateTime tempDateTime;
            if (DateTime.TryParseExact(formCollection[key].Trim(), "yyyy-MM-dd", 
                CultureInfo.InvariantCulture, 
                DateTimeStyles.None, out tempDateTime))
            {
                return tempDateTime;
            }

            return defaultValue;
        }

        public static decimal ParsedDecimal(this FormCollection formCollection, string key)
        {
            decimal tempDecimal;

            if (Decimal.TryParse(formCollection[key].Trim(), out tempDecimal))
            {
                return tempDecimal;
            }

            return decimal.Zero;
            
        }
    }

    public static class StringExtensions
    {
        public static bool IsDefined(this string str)
        {
            return !String.IsNullOrEmpty(str);
        }
    }

    public static class DateTimeExtensions
    {
        public static bool IsDefined(this DateTime? obj)
        {
            return obj.HasValue;
        }
    }

    public static class DecimalExtensions
    {
        public static bool IsDefined(this Decimal? obj)
        {
            return obj.HasValue;
        }
    }

    public static class ByteExtensions
    {
        public static bool IsDefined(this Byte? obj)
        {
            return obj.HasValue;
        }
    }
}